<?php

namespace App\Http\Controllers\admin;

use App\Model\ShopOrder;
use App\Model\ShopOrderFs;
use App\Model\AdminLogsModel;
use App\Model\ShopOrderFsModel;

use App\Model\UserModel;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Http\Controllers\Controller;

//订单列表
class OrderController extends Controller
{
    //订单列表
    public function index(Request $request){
        $list = ShopOrder::where(function ($query) {
			if (request()->keywords) {
				$query->where('sn', request()->keywords);
			}

			if (request()->month) {
				$month_arr = explode("~",request()->month);
				$mintime = $month_arr[0];
				$maxtime = $month_arr[1]." 23:59:59";
				$query->whereBetween('created_at', [$mintime,$maxtime]);
			}

		})->orderBy('created_at','desc')->select('id','sn','all_price','created_at')->paginate(15);
        foreach($list as $k=>$v){
        	$sn_order = ShopOrderFsModel::where('oid',$v['id'])->where('is_delete',2)->select('name','phone')->first();
        	$sn_state = ShopOrderFsModel::where('oid',$v['id'])->where('is_delete',2)->where('state',1)->first();
			$sn_state_exit = '订单内产品,全部已发货';
			if($sn_state){
				$sn_state_exit = '有未发货产品';
			}
			$list[$k]['linkman'] = $sn_order['name'];
			$list[$k]['linktel'] = $sn_order['phone'];
			$list[$k]['sn_state_exit'] = $sn_state_exit;
			$list[$k]['user'] = UserModel::where('id',$v['uid'])->value('user');
		}

        return view('admin.order.index',compact('list','request'));
    }

    //子订单
	public function sn_order(){
    	$id = request()->id;
    	//订单信息
		$sn = ShopOrder::where('id',$id)->value('sn');
    	$list = ShopOrderFsModel::where('oid',$id)->where('is_delete',2)->orderBy('created_at','desc')->select('id','sn_son','shop_name','shop_attr','single_price','num','name','phone','all_price','created_at','state')->paginate(15);

    	return view('admin.order.sn_son',compact('list','sn','id'));
    }

    //订单详情
	public function detail(){
		$id = request()->id;
    	$list = ShopOrderFsModel::where('id',$id)->first();
		$sn = ShopOrder::where('id',$list['oid'])->value('sn');

		if($list['state'] == 1){
			$list['state_name'] = '待发货';
		}elseif($list['state'] == 2){
			$list['state_name'] = '待收货';
		}else{
			$list['state_name'] = '已完成';
		}

    	return view('admin.order.detail',compact('id','list','sn'));
	}

	//订单发货
	public function send(){
    	DB::beginTransaction();
    	try{
    		$order = ShopOrderFsModel::where('id',request()->id)->where('state',1)->first();
    		if(!$order){
    			return ['code'=>0,'msg'=>'该订单有误!'];
			}
    		ShopOrderFsModel::where('id',request()->id)->update([
    			'state'=>2,
				'send_time'=>date('Y-m-d H:i:s'),
				'express_num' => request()->exp_sn,
				'express_com' => request()->exp_company,
			]);
    		DB::commit();
    		$msg = '订单号【'.$order['sn_son'].'】状态更改为已发货';
    		AdminLogsModel::addLog($msg);
    		return ['code'=>1,'msg'=>'已发货!'];
		}catch(Exception $e){
    		DB::rollBack();
    		return ['code'=>0,'msg'=>'发过失败'];
		}
	}
}